<?php
namespace Admin\Controller;
use Think\Controller;
class IndexController extends Controller {
    protected $model;
    public function _initialize(){
        $this->model = D('Admin');
    }
    public function index(){
        redirect(U("Index/center"));
    }
    //后台页面
    public function center(){
        if(empty(session('admin.id'))){//判断是否登录
            redirect(U('index/login/'));
        }
        $this->display("Index/center");
    }
    //前往登录页
    public function login(){
        if(!empty(session('admin.id'))){//判断是否登录
            redirect(U('index/center/'));
        }
        $this->display("Index/login");
    }
    //执行登录
    public function loginpost(){
        $post=I('post.');
        if($this->model->create($post)){
            $map['email']=$post['email'];
            $map['password']=md5($post['password']);
            $result = $this->model->islogin($map);//验证账号密码是否正确
            if($result=='-1'){
                retJson("404","登录失败！");
            }else{
                session('admin',$result);
                retJson("200","登录成功！");
            }
        }else{
            retJson("404",$this->model->getError());
        }
    }
    //前往注册页
    public function register(){
        if(!empty(session('admin.id'))){//判断是否登录
            redirect(U('index/center/'));
        }
        $this->display("Index/register");
    }
    //执行注册
    public function registerpost(){
        $post=I('post.');
        if($this->model->create($post,4)){
            //加邮箱难


            $this->model->add();
            retJson("200","注册成功！");
        }else{
            retJson("404",$this->model->getError());
        }
    }
    //注销用户
    public function logout(){
        $_SESSION=array();
        redirect(U("Index/index"));
    }
}